package com.lxc.infrastructure.persistent.repository;


import com.lxc.domain.operation.model.entity.*;
import com.lxc.domain.operation.repository.IOperationRepository;
import com.lxc.infrastructure.persistent.dao.*;
import com.lxc.infrastructure.persistent.po.*;
import com.lxc.types.common.OperationRequest;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxc18
 * @date 2024/12/27 10:29
 * @description OperationRepository
 */
@Repository
public class OperationRepository implements IOperationRepository {


    @Resource
    IApplicationInterfaceDao applicationInterfaceDao;
    @Resource
    IApplicationInterfaceMethodDao applicationInterfaceMethodDao;
    @Resource
    IApplicationSystemDao applicationSystemDao;
    @Resource
    IGatewayServerDao gatewayServerDao;
    @Resource
    IGatewayDistributionDao gatewayDistributionDao;
    @Resource
    IGatewayServerDetailDao gatewayServerDetailDao;


    @Override
    public List<OPApplicationInterfaceEntity> queryApplicationInterfaceListByPage(OperationRequest<OPApplicationInterfaceEntity> request) {
        List<ApplicationInterface> list = applicationInterfaceDao.queryApplicationInterfaceListByPage(request);
        ArrayList<OPApplicationInterfaceEntity> res = new ArrayList<>(list.size());
        for(ApplicationInterface applicationInterface:list){
            OPApplicationInterfaceEntity opApplicationInterfaceEntity = new OPApplicationInterfaceEntity();
            opApplicationInterfaceEntity.setSystemId(applicationInterface.getSystemId());
            opApplicationInterfaceEntity.setInterfaceId(applicationInterface.getInterfaceId());
            opApplicationInterfaceEntity.setInterfaceName(applicationInterface.getInterfaceName());
            opApplicationInterfaceEntity.setInterfaceVersion(applicationInterface.getInterfaceVersion());
            res.add(opApplicationInterfaceEntity);
        }
        return res;
    }

    @Override
    public int queryApplicationInterfaceListCountByPage(OperationRequest<OPApplicationInterfaceEntity> request) {
        return applicationInterfaceDao.queryApplicationInterfaceListCountByPage(request);
    }

    @Override
    public List<OPApplicationInterfaceMethodEntity> queryApplicationInterfaceMethodListByPage(OperationRequest<OPApplicationInterfaceMethodEntity> request) {
        List<ApplicationInterfaceMethod> methodList = applicationInterfaceMethodDao.queryApplicationInterfaceMethodListByPage(request);
        ArrayList<OPApplicationInterfaceMethodEntity> res = new ArrayList<>(methodList.size());
        for(ApplicationInterfaceMethod method:methodList){
            OPApplicationInterfaceMethodEntity entity = new OPApplicationInterfaceMethodEntity();
            entity.setSystemId(method.getSystemId());
            entity.setInterfaceId(method.getInterfaceId());
            entity.setMethodId(method.getMethodId());
            entity.setMethodName(method.getMethodName());
            entity.setParameterType(method.getParameterType());
            entity.setUri(method.getUri());
            entity.setHttpCommandType(method.getHttpCommandType());
            entity.setAuth(method.getAuth());
            res.add(entity);
        }

        return res;
    }

    @Override
    public int queryApplicationInterfaceMethodListCountByPage(OperationRequest<OPApplicationInterfaceMethodEntity> request) {
        return applicationInterfaceMethodDao.queryApplicationInterfaceMethodListCountByPage(request);
    }

    @Override
    public List<OPApplicationSystemEntity> queryApplicationSystemListByPage(OperationRequest<OPApplicationSystemEntity> request) {
        List<ApplicationSystem> applicationSystems = applicationSystemDao.queryApplicationSystemListByPage(request);
        ArrayList<OPApplicationSystemEntity> res = new ArrayList<>(applicationSystems.size());
        for(ApplicationSystem system:applicationSystems){
            OPApplicationSystemEntity systemEntity = new OPApplicationSystemEntity();
            systemEntity.setSystemId(system.getSystemId());
            systemEntity.setSystemName(system.getSystemName());
            systemEntity.setSystemType(system.getSystemType());
            systemEntity.setSystemRegistry(system.getSystemRegistry());
            res.add(systemEntity);
        }
        return res;
    }

    @Override
    public int queryApplicationSystemListCountByPage(OperationRequest<OPApplicationSystemEntity> request) {
        return applicationSystemDao.queryApplicationSystemListCountByPage(request);
    }

    @Override
    public List<OPGatewayServerEntity> queryGatewayServerListByPage(OperationRequest<String> request) {
        List<GatewayServer> gatewayServers = gatewayServerDao.queryGatewayServerListByPage(request);
        ArrayList<OPGatewayServerEntity> res = new ArrayList<>(gatewayServers.size());
        for(GatewayServer server:gatewayServers){
            OPGatewayServerEntity entity = new OPGatewayServerEntity();
            entity.setGroupId(server.getGroupId());
            entity.setGroupName(server.getGroupName());
            res.add(entity);
        }
        return res;
    }

    @Override
    public int queryGatewayServerListCountByPage(OperationRequest<String> request) {
        return gatewayServerDao.queryGatewayServerListCountByPage(request);
    }

    @Override
    public List<OPGatewayServerDetailEntity> queryGatewayServerDetailListByPage(OperationRequest<OPGatewayServerDetailEntity> request) {
        List<GatewayServerDetail> gatewayServerDetails = gatewayServerDetailDao.queryGatewayServerDetailListByPage(request);
        ArrayList<OPGatewayServerDetailEntity> res = new ArrayList<OPGatewayServerDetailEntity>(gatewayServerDetails.size());
        for(GatewayServerDetail entity:gatewayServerDetails){
            OPGatewayServerDetailEntity opGatewayServerDetailEntity = new OPGatewayServerDetailEntity();
            opGatewayServerDetailEntity.setGroupId(entity.getGroupId());
            opGatewayServerDetailEntity.setGatewayId(entity.getGatewayId());
            opGatewayServerDetailEntity.setGatewayName(entity.getGatewayName());
            opGatewayServerDetailEntity.setGatewayAddress(entity.getGatewayAddress());
            opGatewayServerDetailEntity.setStatus(entity.getStatus());


            res.add(opGatewayServerDetailEntity);
        }
        return res;
    }

    @Override
    public int queryGatewayServerDetailListCountByPage(OperationRequest<OPGatewayServerDetailEntity> request) {
        return gatewayServerDetailDao.queryGatewayServerDetailListCountByPage(request);
    }

    @Override
    public List<OPGatewayDistributionEntity> queryGatewayDistributionListByPage(OperationRequest<OPGatewayDistributionEntity> request) {
        List<GatewayDistribution> gatewayDistributions = gatewayDistributionDao.queryGatewayDistributionListByPage(request);
        ArrayList<OPGatewayDistributionEntity> res = new ArrayList<>(gatewayDistributions.size());
        for(GatewayDistribution gatewayDistribution:gatewayDistributions){
            OPGatewayDistributionEntity opGatewayDistributionEntity = new OPGatewayDistributionEntity();
            opGatewayDistributionEntity.setGroupId(gatewayDistribution.getGroupId());
            opGatewayDistributionEntity.setGatewayId(gatewayDistribution.getGatewayId());
            opGatewayDistributionEntity.setSystemId(gatewayDistribution.getSystemId());
            opGatewayDistributionEntity.setSystemName(gatewayDistribution.getSystemName());

            res.add(opGatewayDistributionEntity);
        }
        return res;
    }

    @Override
    public int queryGatewayDistributionListCountByPage(OperationRequest<OPGatewayDistributionEntity> request) {
        return gatewayDistributionDao.queryGatewayDistributionListCountByPage(request);
    }
}
